<template>
  <div class="add-form">
    <el-dialog
      title="创建提示"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="dialogClear"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item
          label="邮箱"
          prop="email"
        >
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item
          label="角色"
          prop="role"
        >
          <el-input v-model="addForm.role"></el-input>
        </el-form-item>
        <el-form-item label="权限组名称">
          <el-select
            v-model="addForm.permission_group_id"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in permissionList"
              :key="index"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="联系电话"
          prop="phone"
        >
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item
          label="介绍"
          prop="introduction"
        >
          <el-input
            type="textarea"
            :rows="2"
            placeholder="Please input"
            v-model="addForm.introduction"
          ></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addDialogVisible=false">取 消</el-button>
        <el-button
          type="primary"
          @click="addUser"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add as addUser } from '@/api/base/users.js'
export default {
  name: 'usersAdd',
  props: {
    addForm: {
      type: Object,
      required: true
    },
    permissionList: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      addFormRules: {
        // 对用户名进行校验
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        // 对密码进行校验
        password: [
          { required: true, message: '请输入6-15位的密码', trigger: 'blur' }
        ],
        // 对邮箱进行校验
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      },
      selectKeys: [],
      addDialogVisible: false // 控制弹框
    }
  },
  methods: {
    // 弹框显示
    dialogShow () {
      this.addDialogVisible = true
    },
    // 弹框隐藏
    dialogHide () {
      this.addDialogVisible = false
    },
    // 清空输入框
    dialogClear () {
      this.$refs.addFormRef.resetFields()
    },
    // 添加用户功能
    addUser () {
      this.$refs.addFormRef.validate(async valid => {
        try {
          if (!valid) return
          const { data } = await addUser({
            username: this.addForm.username,
            password: this.addForm.password,
            role: this.addForm.role,
            permission_group_id: this.addForm.permission_group_id,
            email: this.addForm.email
          })
          this.$message.success('添加用户成功')
          console.log(data)
          this.$emit('newData')
        } catch (error) {
          console.log(error)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.add-form {
  .demo-ruleForm {
    width: 400px;
    margin-left: 120px;
  }
}
</style>
